package cn.wolfcode.shop.cloud.web.advice;

import cn.wolfcode.shop.cloud.common.CommonAdvice;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.msg.MemberCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.MessageFormat;

@RestControllerAdvice
public class MemberAdviceController extends CommonAdvice {
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerExe(BindException ex){
        //手机号码格式不正确
        String defaultMessage = ex.getAllErrors().get(0).getDefaultMessage();
        //{50004,"参数异常:{0}}
        MemberCodeMsg memberCodeMsg = MemberCodeMsg.PARAMETER_ERROR;
        //{50004,"参数异常:手机号码格式不正确}
        //memberCodeMsg.setMsg(MessageFormat.format(memberCodeMsg.getMsg(),defaultMessage));
        //return Result.Error(memberCodeMsg);
        return Result.Error(memberCodeMsg.FilterArgs(defaultMessage));
    }


}
